package com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.DaiFaHuoAdapter;
import com.lwj.liwanjia.adapter.JinRiDingDanHomeAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.DingBean01;
import com.lwj.liwanjia.bean.JinRiDingDanHomeBean;
import com.lwj.liwanjia.config.OnClickPosition;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.SpfUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/21.
 */

public class DaiFaHuoHomeActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(DaiFaHuoHomeActivity.this, "数据有误", Toast.LENGTH_LONG).show();
                    break;

                case 1:

                    if (homeList != null && homeList.size() > 0) {
                        mRecyclerView.setVisibility(View.VISIBLE);
                        tvNoIndent.setVisibility(View.GONE);
                        LinearLayoutManager manager = new LinearLayoutManager(DaiFaHuoHomeActivity.this);
                        mRecyclerView.setLayoutManager(manager);
                        homeAdapter = new DaiFaHuoAdapter(DaiFaHuoHomeActivity.this, homeList);
                        mRecyclerView.setAdapter(homeAdapter);
                        homeAdapter.setClickPosition(new OnClickPosition() {
                            @Override
                            public void clickPosition(int position) {
                                scrollPosition=position;
                            }
                        });
                        mRecyclerView.scrollToPosition(scrollPosition);
                    }else{
                        mRecyclerView.setVisibility(View.GONE);
                        tvNoIndent.setVisibility(View.VISIBLE);
                    }

                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private DaiFaHuoAdapter homeAdapter;
    private List<DingBean01.DataBean> homeList;
    private RecyclerView mRecyclerView;
    private TextView tvNoIndent;
    private int scrollPosition;

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        tvNoIndent = (TextView) findViewById(R.id.tv_no_indent);
        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("待发货");

        tv_back.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dai_fa_huo_home;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
        }
    }

    //请求网络 获取数据
    private void initOkHttp(String type) {

        String url = URLConstant.BASE_URL + "order/toShowShopOrder";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("status", type)
                .add("token", token)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "分类数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "分类图数据获取成功");

                String result = null;

                try {
                    result = response.body().string();
                    //Log.d("Tag",result);
                    parse01(result);
                } catch (Exception e) {

                }

            }
        });
    }

    //解析
    private void parse01(String result) {

        Gson gson = new Gson();
        DingBean01 bean = gson.fromJson(result, DingBean01.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    homeList = new ArrayList<>();

                    for (int i = 0; i < bean.getData().size(); i++) {

                        DingBean01.DataBean dataBean = bean.getData().get(i);
                        homeList.add(dataBean);
                    }

                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        //请求网络 获取数据
        initOkHttp("待发货");
    }
}
